define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/index' + location.search,
                    edit_url: 'teacher/edit',
                    multi_url: 'teacher/multi',
                    import_url: 'teacher/import',
                    dragsort_url: '',
                    table: 'teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                showExport: false,
                showToggle: false,
                exportTypes : ['excel'],
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id',operate: 'LIKE', title: __('User_id'), width: '200',formatter:function(v,row){
                                var html = '<div style="display:flex"><img width="60px" height="60px" src="'+Fast.api.cdnurl(row.user.avatar)+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.user.id+'<br/>'+row.user.nickname+'<br/>'+row.user.mobile+'</p></div>';
                                return html;
                            }
                        },
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), searchable: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'star', title: __('Star'), operate:'BETWEEN'},
                        {field: 'subject_text',searchable: false, title: __('Subject_id')},
                        {field: 'grade_text',searchable: false, title: __('Grade_id')},
                        {field: 'audit.unholiday_pick',title:'非寒暑授课区域',operate: 'LIKE'},
                        {field: 'age_range_text',searchable: false, title: __('Age_range_id')},
                        {field: 'sex', title: __('Sex'),searchList: {'女':'女','男':'男'}},
                        {
                            field: 'recommend',
                            title: __('优选名师'),
                            align: 'center',
                            searchList: {0:'否',1:'是'},
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
                        {field: 'need_audit',searchable: false, title: __('是否需要审核'), formatter:function(value,row,index){
                            if(value){
                                return '<span style="color: red;">是</span>';
                            }else{
                                return '否';
                            }
                            }, operate: 'LIKE'},
                        {
                            field: 'weigh', // weigh 列
                            title: '排序',
                            editable: true, // 使该列可编辑
                            sortable: true, // 可排序
                            align: 'center', // 居中显示
                            formatter: function (value, row, index) {
                                // 格式化编辑框，显示当前值
                                return '<input style="width: 40px;outline: none;text-align: center;border:1px solid #939394;border-radius: 4px;" type="text" class="weigh-input" value="' + value + '" data-id="' + row.id + '" />';
                            },
                            events: {
                                'blur .weigh-input': function (e, value, row, index) {
                                    // 当输入框失去焦点时更新 weigh
                                    var newWeigh = $(this).val();
                                    var id = row.user_id;

                                    // 发送 AJAX 请求更新 weigh 值
                                    Fast.api.ajax({
                                        url: Fast.api.fixurl('teacher/updateWeigh'),
                                        type: 'POST',
                                        loading: false,
                                        data: {
                                            id: id,
                                            weigh: newWeigh
                                        },
                                        success: function (response) {
                                            if(response.code==1){
                                                Fast.api.msg('更新成功');
                                            }else{
                                                Fast.api.msg('更新失败');
                                            }
                                        },
                                        error: function () {
                                            Fast.api.msg('请求失败');
                                        }
                                    });
                                }
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {
                                    name: 'apply',
                                    text: '入驻申请',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["70%", "80%"]\'',
                                    url: function (row) {
                                        return `teacher_audit/index?teacher_id=${row.user_id}`;
                                    }
                                },
                                {
                                    name: 'orders',
                                    text: function (row){
                                        return '订单x'+row.order_count;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    visible: function (row){
                                        if(row.order_count>0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    extend: 'data-area=\'["70%", "80%"]\'',
                                    url: function (row) {
                                        return `teacher_order/index?teacher_id=${row.user_id}`;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 自定义导出按钮
            $(document).on("click", ".btn-myexcel-export", function () {
                var selectedIds = table.bootstrapTable('getSelections').map(function(row) {
                     return row.user_id;
                });
                console.log(selectedIds);

                layer.confirm("请选择导出的选项", {
                    title: '导出数据',
                    btn: ['全部数据','当前勾选'],
                    yes: function (index, layero) {
                        // 全部数据
                        window.open(Fast.api.fixurl('teacher/export?data=all'),'_blank');
                        Layer.close(index);
                    },
                    btn2: function (index, layero) {
                        if(selectedIds.length==0){
                            Fast.api.msg('至少勾选一位教师');
                            return;
                        }
                        Fast.api.ajax({
                            url: 'teacher/redis_export',
                            loading: false,
                            data: { export_ids: selectedIds },
                            type: 'POST',
                            success: function (data) {
                                console.log(data);
                                window.open(Fast.api.fixurl('teacher/export'),'_blank');
                            },
                            error: function (data) {
                            }
                        });
                    }
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
